//后台初始化
const express = require("express");
const app = express();
// 设置默认端口
app.listen(3000, () => {
  console.log("http://127.0.0.1:3000");
});
// path 路径处理
const path = require("path");
// get 请求处理
const url = require("url");
//formdata表单提交插件
const multer = require("multer");
const upload = multer({ dest: path.join(__dirname, "uploads/") }).single(
  "wallpaper"
);
//跨域处理
const cors = require("cors");
app.use(cors());
//静态资源处理
// app.use("/libs",express.static(path.join(__dirname, './libs')))
app.use("/", express.static(path.join(__dirname, "./www")));
app.use(express.static(path.join(__dirname, "./uploads")));
//导入数据库函数
const bd = require("./dbsql");
const { selectOne, selectAll, add, edit, del } = bd;

//查询单条
app.get("/one", function(req, res) {
  const { id } = req.query;
  selectOne({
    id,
    success(data) {
      res.send({
        code: 200,
        msg: "查询成功",
        data: data[0]
      });
    },
    fail(err) {
      res.send({
        code: 400,
        msg: "参数错误"
      });
    }
  });
});
//查询全部
app.get("/all", function(req, res) {
  selectAll({
    success(data) {
      res.send({
        code:200,
        msg:'获取成功',
        data
      });
    },
    fail(err) {
      res.send(
        {
          code:400,
          msg:'获取失败',
          err
        }
      );
    }
  });
});
//增加信息
app.post("/add", function(req, res) {
  upload(req, res, function(err) {
    if (err instanceof multer.MulterError) {
      res.send({
        code: 400,
        msg: "参数错误"
      });
    } else if (err) {
      res.send({
        code: 400,
        msg: "参数错误"
      });
    } else {
      console.log(req);
      //获取图片
      const { filename: wallpaper } = req.file;
      //获取信息
      const { name, skill, tale } = req.body;

      add({
        name,
        skill,
        tale,
        wallpaper,
        success(data) {
          res.send({
            code: 200,
            msg: "添加成功"
          });
        },
        fail(err) {
          res.send({
            code: 400,
            msg: "参数错误"
          });
        }
      });
    }
  });
});
//编辑信息
app.post("/edit", function(req, res) {
  upload(req, res, function(err) {
    if (err instanceof multer.MulterError) {
      res.send({
        code: 400,
        msg: "参数错误"
      });
    } else if (err) {
      res.send({
        code: 400,
        msg: "参数错误"
      });
    } else {
      console.log(req);
      //获取图片
      const { filename: wallpaper } = req.file;
      //获取信息
      const { id, name, skill, tale } = req.body;

      edit({
        id,
        name,
        skill,
        tale,
        wallpaper,
        success(data) {
          res.send({
            code: 200,
            msg: "编辑成功"
          });
        },
        fail(err) {
          res.send({
            code: 400,
            msg: "参数错误"
          });
        }
      });
    }
  });
});
//删除单条
app.get("/del", function(req, res) {
  const { id } = req.query;
  del({
    id,
    success(data) {
      res.send({
        code: 200,
        msg: "删除成功"
      });
    },
    fail(err) {
      res.send({
        code: 400,
        msg: "删除失败"
      });
    }
  });
});
